<template>
    <div class="template_container">
        <div class="btn_box">
            <!-- <el-button type="primary" @click.stop="goBack">保存</el-button> -->
            <el-button @click.stop="goBack">返回</el-button>
        </div>
        <div class="assess_form swallow">
            <div class="form_title">XXXX医疗机构</div>
            <div class="form_name">康复治疗记录单</div>
            <div class="form_patient_bar">
                <div v-for="(item, index) in patientStruct" :key="index" class="patient_info_item">
                    <div class="item_label">{{ item.label }}：</div>
                    <div class="item_value">{{ currentPatient[item.key] }}</div>
                </div>
            </div>
            <el-form ref="assessForm" :model="selfFormData" :rules="rules" label-width="85px">
                <div class="create_info_item flex_item">
                    <div class="item_label">治疗时间：</div>
                    <div class="item_value">
                        <el-date-picker v-model="selfFormData.doctorSignTime" type="datetime" placeholder="选择日期时间" />
                    </div>
                </div>
                <div class="flex_item">
                    <el-input v-model="selfFormData.recoveredDesc" :autosize="{ minRows: 15 }" type="textarea" />
                </div>
                <div v-if="formType !== 'template'" class="assess_create_info right">
                    <div class="create_info_item">
                        <div class="item_label">治疗师签名：</div>
                        <div class="item_value">{{ selfFormData.doctorSign }}</div>
                    </div>
                </div>
                <el-row style="text-align: center; padding-top: 20px">
                    <el-button type="primary" @click.stop="submit" :loading="submitLoading">
                        保存
                    </el-button>
                </el-row>
            </el-form>
        </div>
    </div>
</template>

<script>
import Cookies from "js-cookie";
import { GENDET_LIST, getLabel } from "./enums";
import moment from "moment";
import { recoveredAdd } from "@/api/therapistsSite/therapistsSite";
const defaultForm = {
    doctorSignTime: moment().valueOf(),
    recoveredDesc: "",
    doctorSign: "",
};
export default {
    name: "rehabilitationRecord",
    data() {
        return {
            selfFormData: Object.assign({}, defaultForm),
            rules: {},
            formType: "edit",
            patientInfo: {},
            submitLoading: false,
        };
    },
    computed: {
        patientStruct() {
            return [
                {
                    label: "姓名",
                    key: "patientName",
                },
                {
                    label: "性别",
                    key: "genderName",
                },
                {
                    label: "年龄",
                    key: "age",
                },
                {
                    label: "科室",
                    key: "departmentName",
                },
                {
                    label: "床号",
                    key: "bedNum",
                },
                {
                    label: "住院号",
                    key: "admissionNum",
                },
            ];
        },
        currentPatient() {
            try {
                const info = JSON.parse(Cookies.get("currentPatient"));
                return info
                    ? {
                        ...info,
                        genderName: getLabel(info.gender, GENDET_LIST),
                    }
                    : {};
            } catch (res) {
                return {};
            }
        },
    },
    created() {
        const userInfo = this.$store.state.user.user;
        this.selfFormData.doctorSign = userInfo.realname;
    },
    methods: {
        goBack() {
            this.$router.back();
        },
        getFormData() {
            return new Promise((resolve, reject) => {
                this.$refs.assessForm.validate((valid) => {
                    if (valid) {
                        resolve({
                            ...this.selfFormData,
                        });
                    } else {
                        reject("存在未通过表单校验的数据");
                        return false;
                    }
                });
            });
        },
        submit() {
            this.getFormData().then((data) => {
                this.submitLoading = true;
                recoveredAdd({
                    ...data,
                    reservationId: this.currentPatient.reservationId,
                })
                    .then(() => {
                        this.$notify({
                            title: "保存成功",
                            type: "success",
                            duration: 1500,
                        });
                        this.goBack();
                    })
                    .finally(() => {
                        this.submitLoading = false;
                    });
            });
        },
    },
};
</script>

<style lang="scss" scoped>
@import url("~@/views/assessSys/templates/style.scss");
@import url("~@/views/assessSys/components/style.scss");

.form_assess_item .blod {
    font-weight: bold;
    text-indent: 2rem;
}

.flex_item {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
}
</style>
